package com.zj.inoutput.service.user.impl;

import com.zj.inoutput.common.constant.UserConst;
import com.zj.inoutput.common.constant.UserType;
import com.zj.inoutput.dal.dataobj.User;
import com.zj.inoutput.dal.mapper.UserMapper;
import com.zj.inoutput.model.request.user.UserParam;
import com.zj.inoutput.model.response.user.UserExportDTO;
import com.zj.inoutput.service.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author DuanYu <a>Email:z-houjie@qq.com</a>
 * @date 2022-02-08 15:56
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int insertSelective(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user);
    }


    /**
     * 查询用户信息
     *
     * @param p 查询参数
     * @return 用户列表
     */
    @Override
    public List<UserExportDTO> findExportData(UserParam p) {
        List<User> users = userMapper.selectList();
        return users.stream().map(v -> new UserExportDTO()
                .setId(v.getId())
                .setName(v.getName())
                .setType(Objects.equals(UserType.STAFF.getValue(),v.getType()) ? UserType.STAFF.getDescription() : UserType.CUSTOMER.getDescription())
                .setMobile(v.getMobile())
                .setAvatar(v.getAvatar())
                .setDeleted(Objects.equals(UserConst.DELETED,v.getDeleted()) ? "是" : "否")
                .setCreatedTime(v.getCreatedTime())
                .setUpdatedTime(v.getUpdatedTime())
        ).collect(Collectors.toList());
    }
}
